//
//  InputVertifyView.m
//  1.input
//
//  Created by cherish on 15/11/17.
//  Copyright © 2015年 杨飞龙 . All rights reserved.
//


#import "InputVertifyView.h"

#import "UIViewExt.h"

#define TCOLOR [UIColor colorWithRed:187.0/255.0 green:147.0/255.0 blue:115.0/255.0 alpha:1]
@interface InputVertifyView ()<UITextFieldDelegate>
{
    
    
    
    NSMutableArray *_dataSource;
    
    
}
@end


@implementation InputVertifyView

#pragma mark - init Methods
- (id)initWithFrame:(CGRect)frame
{
    
    self = [super initWithFrame:frame];
    
    if (self) {
       
        
        _dataSource = [[NSMutableArray alloc]init];
        
        
        [self _loadSearchView];
        
    }
    
    return self;
    
}//初始化方法


#pragma mark - Private Methods
- (void)_loadSearchView
{
    
    UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 120*kscale, KScreenWidth, 300)];
    bgView.backgroundColor = [UIColor blackColor];
    
    //请输入手机号码
    UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(10, 80, KScreenWidth-20, 40)];
    lable.text = @"";
    lable.textAlignment = NSTextAlignmentLeft;
    lable.font = [UIFont systemFontOfSize:18];
    [bgView addSubview:lable];
    
    
    CGFloat width = (KScreenWidth-70)/6;
    
    //底部的textfield
    UITextField *backGround = [[UITextField alloc]initWithFrame:CGRectMake(20, 0, KScreenWidth-40, width)];
    self.backGround = backGround;
    self.backGround.hidden = YES;
    self.backGround.keyboardType = UIKeyboardTypeNumberPad;
    self.backGround.delegate = self;
    [self.backGround addTarget:self action:@selector(txChange:) forControlEvents:UIControlEventEditingChanged];
    [self.backGround becomeFirstResponder];
    [bgView addSubview:self.backGround];
    

    //for循环创建6个按钮
    for (int i = 0; i < 6; i++) {
        
        
        UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(35+i*width, 0, width, width)];
        
        
        button.titleLabel.font = [UIFont systemFontOfSize:30.0];
        
        
        [button setTitleColor:TCOLOR forState:UIControlStateNormal];
        
        button.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
        
        [button addTarget:self action:@selector(touchBegin) forControlEvents:UIControlEventTouchUpInside];
        
        button.layer.borderWidth = 0.5;
        button.layer.borderColor = TCOLOR.CGColor;
        [_dataSource addObject:button];
        
        [bgView addSubview:button];
        
        
        
    }
    
    
    
    
    //创建查询按钮
//    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
//    button.frame = CGRectMake(30 , backGround.bottom+20, KScreenWidth-60, 40);
//    [button setTitle:@"查询" forState:UIControlStateNormal];
//    [button setTitleColor:[UIColor colorWithRed:129/255.0 green:135/255.0 blue:140/255.0 alpha:1] forState:UIControlStateNormal];
//    [button addTarget:self action:@selector(clickSearch:) forControlEvents:UIControlEventTouchUpInside];
//    button.layer.borderWidth = 1;
//    button.layer.borderColor =[UIColor colorWithRed:129/255.0 green:135/255.0 blue:140/255.0 alpha:1].CGColor;
//    button.titleLabel.font = [UIFont boldSystemFontOfSize:16];
//    [bgView addSubview:button];
    
    [self  addSubview:bgView];
    
    
    
 
    
    
    
    
    
}//输入手机号码查询视图


- (void)txChange:(UITextField*)tx
{
   
    NSString *text = tx.text;
    if (text.length == _dataSource.count) {
      
        //这里可以自动监听
        NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
        [center postNotificationName:@"VerCodePost" object:text userInfo:nil];
}
    
    
    
    for (int i = 0; i < _dataSource.count; i++) {
        
        
        UIButton *btn = [_dataSource objectAtIndex:i];
        
        
        NSString *str = @"";
        
        if (i < text.length) {
            
   
            str = [text substringWithRange:NSMakeRange(i, 1)];
            
            
        }

        [btn setTitle:str forState:UIControlStateNormal];
    }
 
}//监听，输入文字



- (void)touchBegin
{
    
    [self.backGround becomeFirstResponder];
    
}//成为第一响应者




#pragma mark - Action Methods
- (void)clickSearch:(UIButton*)sender
{
    
    
    NSMutableString *str = [NSMutableString string];
    
    for (int i = 0; i < _dataSource.count; i++) {
        
        
        UIButton *button = _dataSource[i];
        
        
        if (button.currentTitle.length == 0) {
            
            
        }else{
            
            [str appendString:button.currentTitle];
        }
    
    }
    
    
//    if (str.length < 6) {
//        
//        
//        [[[UIAlertView alloc]initWithTitle:@"提示" message:@"请输入用户手机号码后六位查询" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil]show];
//        
//        
//    }else if (str.length == 6){
//    
//        
//     [[[UIAlertView alloc]initWithTitle:@"提示" message:str delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil]show];
//        
//        //这里直接去查询
//        [self.backGround resignFirstResponder];
//
// 
//        if (_clickSearch != nil) {
//            
//            _clickSearch(str);
//        }
//        
//        
//    }
    

    
}//点击查询

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSInteger existedLength = textField.text.length;
    NSInteger selectedLength = range.length;
    NSInteger replaceLength = string.length;
    NSInteger pointLength = existedLength - selectedLength + replaceLength;
    //超过6位 就不能在输入了
    if (pointLength > 6) {
        return NO;
    }else{
        return YES;
    }
    
}

//点击空白区域，隐藏键盘
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    //结束编辑
    [self endEditing:YES];
}




@end
